<?php

namespace App\Repositories\Api;

use App\Models\Content;

class ContentRepository
{
    public function getPages(array $filter = [], int $pageSize = 10, int $pageNum = 1)
    {
        $res = Content::query()->where("status", "=", 2)
            ->when(intval($filter['category_id']), function ($query) use ($filter) {
                return $query->where("category_id", "=", intval($filter['category_id']));
            })
            ->orderBy('publish_time', 'desc')
            ->orderBy('sort', 'asc')
            ->paginate($filter['limit'] ? $filter['limit'] : $pageSize, ["id", "thumb", "summary", "content", "title", "subtitle", "create_time"], null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        return $res;
    }

    public function getOne($id)
    {
        return Content::where("id", "=", intval($id))->first();
    }

}